﻿using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using demo1.Models;
using demo1.Models.Database;

namespace demo1.Service
{
    public class RedisService : IRedisService
    {
        private readonly IDatabase _redis;
        public RedisService(RedisHelper client)
        {
            _redis = client.GetDatabase();
        }

        public void AddGoodViews(int userId, Good good)
        {
            var key = $"UserViews{userId}";

            var jsonStr = JsonConvert.SerializeObject(good);//序列化
            _redis.ListLeftPush(key, jsonStr);// 1 1 1
        }
        public List<Good> GetUserViews(int userId)
        {
            var key = $"UserViews{userId}";
            var data = _redis.ListRange(key, 0, 4);
            var list = new List<Good>();
            foreach (var item in data)
            {
                var good = JsonConvert.DeserializeObject<Good>(item);
                list.Add(good);
            }
            return list;
        }
    }
   
}
